package cn.ihoway.inform;

import cn.ihoway.entity.Allocation;
import cn.ihoway.redis.AllocationRedis;
import cn.ihoway.redis.CharacterRedis;
import cn.ihoway.redis.RoomRedis;
import cn.ihoway.socket.GameSessionManager;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统向玩家发送通知
 */
public class InformUtils {

    /**
     * 向指定房间所有玩家发送通知
     */
    public void toAllByRoomId(String roomId,String content) {
        try{
            ArrayList<WebSocketSession> sessions = GameSessionManager.getFromRoom(roomId);
            for (WebSocketSession session : sessions){
                session.sendMessage(new TextMessage(content));
            }
        }catch (IOException e){
            throw new RuntimeException(e);
        }

    }

    /**
     * 向全部玩家发送通知
     */
    public void toAll(String content){
        try{
            ArrayList<WebSocketSession> sessions = GameSessionManager.getAll();
            for (WebSocketSession session : sessions){
                session.sendMessage(new TextMessage(content));
            }
        }catch (IOException e){
            throw new RuntimeException(e);
        }
    }

}
